<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2023/6/17
 * Time: 15:07
 */
namespace app\admin\controller;

use app\middleware\AdminCheck;
use think\facade\Db;

class Cardriver {

    protected $middleware = [
        AdminCheck::class => ['except' 	=> [] ]
    ];

    /**
     * @return \think\response\Json
     */
    public function store() {
        validateParam('name,mobile,id_card_font,id_card_back,license,region_id','name,mobile,id_card_font,id_card_back,license,region_id');
        // 车辆编号生成规则
        //1、由10位啊拉伯数字组成，随机生成。
        //2、同车牌一样，具有唯一性。
        $data['name']=input('name');
        $data['mobile']=input('mobile');
        $data['id_card_font']=input('id_card_font');
        $data['id_card_back']=input('id_card_back');
        $data['license'] = input('license');
        if (!empty(input('region_id'))) {
            $data['region_id'] = implode(',', input('region_id'));
        }

        if (!input('id')) {
            $where = [
                'mobile' => input('mobile'),
            ];
            if (Db::name('car_drivers')->where($where)->find()) {
                return error('试驾员已存在');
            }
            $data['status'] = 2;
            $data['source'] = 0;
            $res = Db::name('car_drivers')->insert($data);
        } else {
            $where = [
                'mobile' => input('mobile'),
                ['id', '!=', input('id')]
            ];
            if (Db::name('car_drivers')->where($where)->find()) {
                return error('试驾员已存在');
            }
            $res = Db::name('car_drivers')->where('id', input('id'))->update($data);
        }
        return success('添加成功', $res);
    }

    /**
     * 试驾员列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList(){
        validateParam('pageNo,pageSize','pageNo,pageSize');
        $param = input();
        $where = [];

        $query = Db::name('car_drivers')
//            ->alias('a')
//            ->leftJoin('user b','a.user_id=b.id')
            ->where($where);
        if (isset($param['name']) && !empty($param['name'])) {
            $query->whereLike('name', $param['name']);
        }
        if (isset($param['source']) && is_numeric($param['source'])) {
            $query->where('source', $param['source']);
        }
        $total = $query->count();

        $offset = makeOffset($param['pageNo'],$param['pageSize']);
        $list = $query
            ->field('*')
            ->limit($offset,$param['pageSize'])
            ->where($where)
            ->order('id desc')
            ->select()->toArray();

        foreach ($list as &$value) {
            $value['driver_times'] = Db::table('car_driver_orders')->where(['is_deleted' => 0, 'car_driver_id' => $value['id']])->count();
            if (!empty($value['region_id'])) {
                $regionIds = explode(',', $value['region_id']);
                $value['regions'] = [];
                if (!empty($regionIds)) {
                    $value['regions'] = Db::name('region')->whereIn('region_id', $regionIds)->field(['region_id', 'region_name'])->select();
                }
            }
        }

        return success('试驾员列表',['total'=>$total,'list'=>$list]);
    }

    /**
     * 更新试驾员状态
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function updateStatus() {
        $params = validateParam('id,status','id,status');
        Db::name('car_drivers')->where('id', input('id'))->update(['status' => input('status', 0), 'refuse_reason' => input('refuse_reason', '')]);
        return success('更新成功');
    }

    /**
     * 试驾员订单列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function driverOrderList(){
        validateParam('id,pageNo,pageSize','id,pageNo,pageSize');
        $param = input();
        $where = ['car_driver_id' => input('id')];

        $query = Db::name('car_driver_orders')
                ->alias('a')
                ->leftJoin('pay_order o','a.order_id=o.id')
                ->where($where);
//        if (isset($param['name']) && !empty($param['name'])) {
//            $query->whereLike('name', $param['name']);
//        }
//        if (isset($param['source']) && is_numeric($param['source'])) {
//            $query->where('source', $param['source']);
//        }
        $total = $query->count();

        $offset = makeOffset($param['pageNo'],$param['pageSize']);
        $list = $query
            ->field('a.car_no, a.status,o.order_no,o.order_name,o.address,o.pay_money,o.comment')
            ->limit($offset,$param['pageSize'])
            ->where($where)
            ->order('a.id desc')
            ->select()->toArray();

        return success('试驾员订单列表',['total'=>$total,'list'=>$list]);
    }
}